package week8;

/**
 * Created by Administrator on 2016/4/23.
 */
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;

public class NIOUtil {
    public static void dump(ReadableByteChannel src, WritableByteChannel dest) throws IOException {
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        try (ReadableByteChannel srcCH = src; WritableByteChannel destCH = dest) {
            while (srcCH.read(buffer) != -1) {
                buffer.flip();
                destCH.write(buffer);
                buffer.clear();
            }
        }
    }

    public static void main(String[] args) throws Exception {
        URL url = new URL("http://openhome.cc");
        ReadableByteChannel src = Channels.newChannel(url.openStream());
        WritableByteChannel dest = new FileOutputStream("index.html").getChannel();
        NIOUtil.dump(src, dest);
    }
}
